package com.TestSocket;
 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.TestSocket.TestSocketService.OnSocketService;
 

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestSocketActivity extends Activity implements View.OnClickListener,OnSocketService{
	public static final String  Tag = "testSocket";
	public static TextView mTV_ServiceStatus;
	public static TextView mTV_ClientMsg;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btnStartService = (Button)findViewById(R.id.BTN_StartService);
        btnStartService.setOnClickListener(this);
        
        Button btnConnectService = (Button)findViewById(R.id.BTN_ConnectService);
        btnConnectService.setOnClickListener(this);        

        Button btnSendMsg = (Button)findViewById(R.id.BTN_SendMsg);
        btnSendMsg.setOnClickListener(this);   
        
        mTV_ServiceStatus = (TextView)findViewById(R.id.TV_SERVICESTATE);
        mTV_ClientMsg = (TextView)findViewById(R.id.TV_CLIENTMSG);
        
        initClient();
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
			case R.id.BTN_StartService:
				mTV_ServiceStatus.setText("Yes,Has connected");
				startService();	
				break;
			
			case R.id.BTN_ConnectService:
				connectService();
				break;
			case R.id.BTN_SendMsg:
				sendMessage();
				break;
		}
	}
	
 
	
	public void startService()
	{
		TestSocketService thSrv = new  TestSocketService(this,this);
		thSrv.start();
	}

	public static final String Host = "127.0.0.1";
	public Socket mClientSocket=null;
	public BufferedReader mClientIn=null;
	public String Content = "";
	public PrintWriter mOut=null;
	public void initClient()
	{
		
	}
	
	public void connectService(){
		try {
			mClientSocket = new Socket(Host,TestSocketService.Connect_Port);
			mClientIn = new BufferedReader(new InputStreamReader(mClientSocket.getInputStream()));
			mOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(mClientSocket.getOutputStream())),true);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendMessage()
	{
		if(mClientSocket.isConnected())
		{
			if(!mClientSocket.isOutputShutdown()){
				mOut.println("exit");
			}
		}
	}

	@Override
	public void onSocketServiceClientExit(String msg) {
		// TODO Auto-generated method stub
		mTV_ClientMsg.setText(msg);
	}

	@Override
	public void onSocketServiceClientConnect(String msg) {
		// TODO Auto-generated method stub
		mTV_ClientMsg.setText(msg);
	}
}
































